﻿using AiMoWindows.CodeScripts.IO;
using AiMoWindows.Localization;
using Microsoft.Win32;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace AiMoWindows.Windows.SaveCode
{
    /// <summary>
    /// SaveCodeWindow.xaml 的交互逻辑
    /// </summary>
    partial class SaveCodeWindow : Window
    {
        string DefaultName => "AiMoScripts_" + DateTime.Now.ToString("MMdd_HHmm");
        readonly ShareSaveResult result;
        readonly SaveMode saveMode;
        string CurrentSaveFolder = "";
        public SaveCodeWindow(SaveMode mode, ShareSaveResult result)
        {
            this.result = result;
            saveMode = mode;
            CurrentSaveFolder = ExternalSettings.ScriptsFolderWithHistory;
            InitializeComponent();
            NameBox.TextChanged += NameBox_TextChanged;

            SetLanguage();
            ExternalSettings.Load();
        }

        private void NameBox_TextChanged(object sender, TextChangedEventArgs e)
        {

            if (saveMode != SaveMode.Share) UpdatePath();
            if (NameBox.Text.Length > 30) NameBox.Text = NameBox.Text[..30];
        }

        void UpdatePath()
        {
            string name = NameBox.Text;
            if (string.IsNullOrEmpty(name)) name = DefaultName;

            PathText.Text = Path.Combine(CurrentSaveFolder, name + CodeIO.Extension);
            FileInfo info = new FileInfo(PathText.Text);
            if (info.Exists && info.IsReadOnly)
            {
                MessageBox.Show(this, Local.Get("Cannot override readonly file") + Environment.NewLine + info.FullName);
                return;
            }
        }
        bool IsEnglish => !Local.RunningInChinese;
        void SetLanguage()
        {
            FontFamily = new FontFamily(IsEnglish ? "Bahnschrift Condensed" : "Microsoft Yahei");

            DescriptionBox.Text = result.Description;
            AuthorBox.Text = result.Author;
            NameBox.Text = string.IsNullOrEmpty(result.CodeName) ? DefaultName : result.CodeName;


            if (saveMode == SaveMode.Share)
            {
                ChangePathButton.Visibility = Visibility.Hidden;
                PathText.Visibility = Visibility.Hidden;
                ConfirmButton.Content = IsEnglish ? "Share " : "分享";
                ShareKeyLabel.Text = IsEnglish ? "ShareKey : " : "分享码：";
                ShareKeyText.Text = result.ShareKey;
            }
            else
            {
                ShareKeyText.Visibility = Visibility.Hidden;
                ShareKeyLabel.Visibility = Visibility.Hidden;
                ConfirmButton.Content = IsEnglish ? "Save " : "保存";
                PathText.Text = Path.Combine(CurrentSaveFolder, NameBox.Text + CodeIO.Extension);

            }
            NameText.Text = IsEnglish ? "Name" : "名字";
            DescriptionText.Text = IsEnglish ? "Description " : "说明";
            ChangePathButton.Content = IsEnglish ? "Change Save Path" : "更改路径";
            AutohrText.Text = IsEnglish ? "Author" : "作者";

            DescriptionBox.Text = result.Description;
            AuthorBox.Text = result.Author;
            NameBox.Text = string.IsNullOrEmpty(result.CodeName) ? DefaultName : result.CodeName;
        }
        private void ChangePathButton_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog()
            {
                InitialDirectory = CurrentSaveFolder,
                RestoreDirectory = true,
                DefaultExt = CodeIO.Extension,
                Filter = $"AiMo Script File|*{CodeIO.Extension}",
                Title = Local.Get("Save AiMo Script"),
                FileName = NameBox.Text + CodeIO.Extension,
            };
            if (saveFileDialog.ShowDialog() == true)
            {
                NameBox.Text = Path.GetFileNameWithoutExtension(saveFileDialog.FileName);
                CurrentSaveFolder = Path.GetDirectoryName(saveFileDialog.FileName) ?? "";
                ExternalSettings.ScriptsFolderWithHistory = CurrentSaveFolder;
                CodeIO.RepairExtensions(CurrentSaveFolder);
                ExternalSettings.Save();
                UpdatePath();
            }

        }

        private void ConfirmButton_Click(object sender, RoutedEventArgs e)
        {
            string name = NameBox.Text.Trim();
            if (string.IsNullOrWhiteSpace(name))
            {
                MessageBox.Show(Local.Get("Code name cannot be empty."));
                return;
            }

            if (name.Length < 4)
            {
                MessageBox.Show(Local.Get("Code name must have more than 4 characters."));
                return;
            }

            if (saveMode != SaveMode.Share) result.SavePath = PathText.Text;
            result.OK = true;
            result.Description = DescriptionBox.Text;
            result.Author = AuthorBox.Text;
            result.CodeName = name;
            Close();
        }
    }
}
